package com.lgx.authorize.module.rest;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.authorize.module.entity.ApplicationApplykeyInfo;
import com.lgx.authorize.module.entity.AuthorizeUserInfo;
import com.lgx.authorize.module.service.IApplicationApplykeyInfoService;
import com.lgx.authorize.module.service.IAuthorizeUserInfoService;
import com.lgx.right.entity.User;
import com.lgx.right.service.IUserService;

/**
 * this support authorize login
 * @author pengdeyi (king)
 *
 */
@RequestMapping("/lgxauth")
@Controller
public class AuthorizeLoginController {
	/**
	 * logger
	 */
	private static final Logger logger = Logger.getLogger(AuthorizeLoginController.class);
	
	@Autowired
	IUserService userService;
	
	@Autowired
	IAuthorizeUserInfoService authorizeUserInfoService;
	
	@Autowired
	IApplicationApplykeyInfoService applicationApplykeyInfoService;

	/**
	 * this support login token.
	 * this url is /lgxauth/authorize/key/XXXXX/name/XXXX/u/XXX/p/XXX/t/XXX
	 * @param appkey
	 * @param appName
	 * @param userName
	 * @param password
	 * @param timeLine
	 * @return
	 */
	@RequestMapping(value="/authorize/key/{key}/name/{name}/u/{u}/p/{p}/t/{t}",method=RequestMethod.GET)
	@ResponseBody
	public Object authorize(@PathVariable("key") String appkey,@PathVariable("name") String appName,
			@PathVariable("u") String userName,@PathVariable("p") String password
			,@PathVariable("t") String timeLine){
		String ret = null;
		User user = userService.loginUser(userName, password);
		ApplicationApplykeyInfo applicationApplykeyInfo = null;
		if(StringUtils.isBlank(appkey)&&StringUtils.isBlank(appkey)){
			if(logger.isInfoEnabled()){
				logger.info("app key and app name is null so interrupt the application");
			}
			ret = "you must set app key and app name.";
		}else{
			applicationApplykeyInfo = applicationApplykeyInfoService.findApplicationApplykeyInfoByNameKey(appName, appkey);
		}
		if(StringUtils.isBlank(timeLine)){
			if(logger.isInfoEnabled()){
				logger.info("the timeline paramter is not null");
			}
			ret = "you must set the timeline";
		}else if(applicationApplykeyInfo!=null){
			if(user!=null){
				AuthorizeUserInfo info = authorizeUserInfoService.findAuthorizeUserInfoByUser(user);
				if(info!=null){
					if(info.getEndTime().before(new Date(Long.parseLong(timeLine)))){
						ret = info.getAuthorizeToken();
					}else{
						info.setStartTime(new Date());
						info.setEndTime(applicationApplykeyInfo.getDisableTime());
						authorizeUserInfoService.updateAuthorizeUserInfo(info);
					}
				}else{
					AuthorizeUserInfo infoUser = authorizeUserInfoService.authorizeUserForApplication(user, applicationApplykeyInfo);
					ret = infoUser.getAuthorizeToken();
				}
			}else{
				if(logger.isInfoEnabled()){
					logger.info("try login use key : "+appkey+" loginname : "+userName+" failed can't find user.");
				}
				ret = "can't find user please check your account.";
			}
		}
		return ret;
	}
	
}

